{[{define "sys/auth/auth.html"}]}
{[{template "sys/base/mark.html"}]}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link rel="stylesheet" href="/pear-admin-layui/component/pear/css/pear.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="name" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="auth-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <!--<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>-->
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script type="text/html" id="power-bar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <!--<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>-->
</script>

<script type="text/html" id="power-type">
    {{#  layui.each(marshal, function(index, item){ }}
    {{#  if(item.dataValue == d.type){ }}
    {{item.dataLabel}}
    {{#  } }}
    {{#  }); }}
</script>

<script type="text/html" id="power-enable">
    {[{.enable}]}
</script>

<script type="text/html" id="icon">
    <i class="layui-icon {{d.icon}}"></i>
</script>

<script type="text/html" id="createBy">
    {{#if (d.createTime) { }}
    {{d.createName}}（{{layui.util.toDateString(d.createTime*1000, 'yyyy-MM-dd HH:mm:ss')}}）
    {{# } }}
</script>

<script type="text/html" id="updateBy">
    {{#if (d.updateTime) { }}
    {{d.updateName}}（{{layui.util.toDateString(d.updateTime*1000, 'yyyy-MM-dd HH:mm:ss')}}）
    {{# } }}
</script>

<script src="/pear-admin-layui/component/layui/layui.js"></script>
<script src="/pear-admin-layui/component/pear/pear.js"></script>
<script>

    let marshal = JSON.parse({[{.authTypes}]})

    layui.use(['table', 'form', 'jquery', 'treetable', 'loading'], function () {
        let table = layui.table;
        let $ = layui.jquery;
        let form = layui.form;
        let loading = layui.loading;
        let treetable = layui.treetable;

        window.render = function () {
            treetable.render({
                treeColIndex: 2,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'pid',
                skin: 'line',
                treeDefaultClose: true,
                toolbar: '#power-toolbar',
                elem: '#power-table',
                url: '/sys/auth/table',
                page: false,
                method: 'post',
                cols: [
                    [
                        {field: 'id', title: 'id', hide: true},
                        {field: 'pid', title: 'pid', hide: true},
                        {field: 'name', title: 'name'},
                        {field: 'url', title: 'url'},
                        {field: 'code', title: 'code'},
                        {field: 'icon', title: 'icon', templet: '#icon'},
                        {field: 'type', title: 'type', templet: '#power-type'},
                        {field: 'enable', title: 'enable', templet: '#power-enable'},
                        {field: 'sort', title: 'sort'},
                        {title: 'remark', field: 'remark', align: 'center', hide: true},
                        {title: 'createBy', field: 'createBy', align: 'center', templet: '#createBy', hide: true},
                        {title: 'updateBy', field: 'updateBy', align: 'center', templet: '#updateBy', hide: true},
                        {title: '<i class="layui-icon layui-icon-set">', templet: '#power-bar', align: 'center'}
                    ]
                ]
            });
        }

        render();

        table.on('tool(power-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'add') {
                window.add(obj);
            }
        })

        form.on('switch(power-enable)', function (obj) {
            // layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
            loading.Load(1, "loading data ...");
            $.post('/sys/auth/enable', {id: obj.value, enable: obj.elem.checked}, function (r) {
                loading.loadRemove(1000);
                if (r.code === 200) {
                    layer.msg(r.msg, {
                        icon: 1,
                        time: 1000
                    });
                } else {
                    layer.msg(r.msg, {
                        icon: 2,
                        time: 1000
                    });
                }
            });
        });

        table.on('toolbar(power-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'expandAll') {
                treetable.expandAll("#power-table");
            } else if (obj.event === 'foldAll') {
                treetable.foldAll("#power-table");
            }
        });

        window.add = function (data) {
            console.log(data)
            layer.open({
                type: 2,
                title: 'add_auth',
                shade: 0.1,
                area: ['450px', '660px'],
                content: '/sys/auth/add' + (data ? '?pid=' + data.data.id : '')
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['450px', '660px'],
                content: '/sys/auth/edit/' + obj.data.id
            });
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该权限', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "remove/" + obj.data['powerId'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        form.on('submit(auth-query)', function (data) {
            console.log(data)
            treetable.reload('#power-table', {
                where: data.field
            })
            return false;
        });

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].powerId + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些权限', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>
{[{end}]}